<template>
  <a-card :bordered="false">
    <div style="overflow-x: scroll;">
      <div v-if="showConditionFlag" style="margin-bottom: 5px;display: flex;width: max-content" >
        当前输入条件：
        <span v-show="this.queryParam.name != null && this.queryParam.name !== ''" style="margin-right: 20px">姓名 / {{this.queryParam.name}}</span>
        <span v-show="this.queryParam.phone != null && this.queryParam.phone !== ''" style="margin-right: 20px">电话 / {{this.queryParam.phone}}</span>
        <span v-show="this.queryParam.address != null && this.queryParam.address !== ''" style="margin-right: 20px">项目地址 / {{this.queryParam.address}}</span>
        <span v-show="this.currentProgressValue != null && this.currentProgressValue != 0" style="margin-right: 20px">项目进展 / {{getProgressValue(this.currentProgressValue)}}</span>
        <span v-show="this.queryParam.scrapOrderCause != null && this.queryParam.scrapOrderCause != ''">废单原因 / {{getScrapOrderCause(this.queryParam.scrapOrderCause)}}</span>
      </div>
    </div>
    <van-search placeholder="请选择条件查询" readonly @click="showSearchPopup" style="width: 100%;padding: 0px;margin-bottom: 10px"/>
    <a-button type="primary" @click="searchReset" icon="reload" style="width: 100%;margin-bottom: 10px">重置</a-button>
    <div>
      <div class="list">
        <van-list
          v-model="loading"
          :finished="finished"
          offset="100"
          finished-text="没有更多了"
          @load="onLoadList"
          :immediate-check="false"
        >
          <div
            class="customer-list"
            v-for="(item,i) in dataSource"
            :key="i"
            @click="followRecordDetail(item)"
          >
            <div>
              <div class="name">客户姓名：{{ item.name || '无' }}</div>
              <div class="contact">
                <span>项目地址：{{ item.address || '无' }}</span>
                <span style="margin-left: 20px">电话：{{ item.phone || '无' }}</span>
              </div>
              <div class="contact">
                <span>废单原因：{{ getScrapOrderCause(item.scrapOrderCause) }}</span>
                <span style="margin-left: 20px">项目进展：{{getProgressValue(item.progress)}}</span>
              </div>
              <div class="contact">
                <span>公海库类型：废单公海库</span>
              </div>
              <div class="contact">
                <span>最近跟进时间：{{ item.latelyFollowTime || '无' }}</span>
              </div>
              <div class="contact">
                <span>最近跟进内容：{{ item.latelyFollowContent || '无' }}</span>
              </div>
            </div>
          </div>
        </van-list>
      </div>
    </div>
    <van-popup v-model="showSearchPopupFlag" position="top" >
      <a-card :bordered="false">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <div>客户姓名：<a-input placeholder="请输入客户姓名" v-model="queryParam.name"></a-input></div>
          <div style="margin-top: 5px">电话：<a-input placeholder="请输入客户电话" v-model="queryParam.phone"></a-input></div>
          <div style="margin-top: 5px">项目地址：<a-input placeholder="请输入项目地址" v-model="queryParam.address"></a-input></div>
          <div style="margin-top: 5px">当前进展：
            <van-dropdown-menu>
              <van-dropdown-item v-model="value1" :options="progressOption" @change="changeProgress"/>
            </van-dropdown-menu>
          </div>
          <div style="margin-top: 5px">废单原因：
            <van-dropdown-menu>
              <van-dropdown-item v-model="value2" :options="option2" @change="changeScrapOrderCause"/>
            </van-dropdown-menu>
          </div>
          <a-row :gutter="24" style="margin-bottom: 10px;margin-top: 10px">
            <a-col :span="12">
              <a-button type="primary" style="width: 100%" @click="searchQuery" icon="search">查询</a-button>
            </a-col>
            <a-col :span="12">
              <a-button type="primary" @click="searchReset" icon="reload" style="width: 100%">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </a-card>
    </van-popup>
    <customer-follow-record-modal ref="customerFollowRecordModal"></customer-follow-record-modal>
  </a-card>
</template>

<script>

  import '@assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { postAction, getAction } from '@api/manage'
  import SelectUserModal from '../../info/tab/modules/SelectUserModal'
  import customerFollowRecordModal from '../modules/customerFollowRecordModal'
  import { filterDictTextByCache } from '@comp/dict/JDictSelectUtil'

  export default {
    name: 'ScrapOrderHighSeasTab',
    mixins: [JeecgListMixin, mixinDevice],
    components: {
      SelectUserModal,
      customerFollowRecordModal
    },
    data() {
      return {
        description: 'gh_customer_submit管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function (t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '客户姓名',
            align: 'center',
            dataIndex: 'name',
            scopedSlots: {
              customRender: 'name'
            },
          },
          {
            title: '项目地址',
            align: 'center',
            dataIndex: 'address'
          },
          {
            title: '电话',
            align: 'center',
            dataIndex: 'phone',
            customRender: function (text, record) {
              if (record.highSeasDepot != 2) {
                return text
              } else {
                if (text != null && text !== '') {
                  return text.toString().slice(0, 3) + '****' + text.toString().slice(-4)
                }
              }
            }
          },
          {
            title: '公海库类型',
            align: 'center',
            dataIndex: 'highSeasDepot',
            customRender: function (text) {
              switch (text) {
                case 1:
                  return '海捞公海库'
                case 2:
                  return '遗单公海库'
                case 3:
                  return '废单公海库'
                case 4:
                  return '个人公海库'
              }
            }
          },
          {
            title: '废单原因',
            align: 'center',
            dataIndex: 'scrapOrderCause',
            customRender: function (text) {
              switch (text) {
                case 1:
                  return '只是咨询'
                case 2:
                  return '信息有误'
                case 3:
                  return '同行抢单'
                case 4:
                  return '价格太高'
                case 5:
                  return '设计不理想'
                case 6:
                  return '跟进超时'
              }
            }
          },
          {
            title: '最近跟进时间',
            align: 'center',
            dataIndex: 'latelyFollowTime',
            customRender: function (text) {
              return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
            }
          },
          {
            title: '最近跟进内容',
            align: 'center',
            dataIndex: 'latelyFollowContent',
            width: '700px',
            customCell: () => {
              return {
                style: {
                  wordWrap: 'break-word',
                  wordBreak: 'break-all',
                  whiteSpace: 'normal',
                  minHeight: '50px',
                  width: '700px',
                }
              }
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            fixed: 'right',
            width: 147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: '/ghCustomerSubmit/list',
          delete: '/ghCustomerSubmit/delete',
          deleteBatch: '/ghCustomerSubmit/deleteBatch',
          exportXlsUrl: '/ghCustomerSubmit/exportXls',
          importExcelUrl: 'ghCustomerSubmit/importExcel',
          getItemsByDictCodeUrl: '/ghCustomerInfo/getItemsByDictCode',
        },
        dictOptions: {},
        loading: false,
        selectedRowKeys: [],
        selectedRows: [],
        progressList: [],
        currentProgressValue: '',
        currentCustomerObj: {},
        queryParam:{},
        progress:{
          show:false,
          text:'全部',
        },
        finished: false,
        value1: "0",
        progressOption: [],
        value2: "",
        option2: [
          { text: '全部', value: "" },
          { text: '只是咨询', value: 1 },
          { text: '信息有误', value: 2 },
          { text: '同行抢单', value: 3 },
          { text: '价格太高', value: 4 },
          { text: '设计不理想', value: 5 },
          { text: '跟进超时', value: 6 },
        ],
        showSearchPopupFlag: false,
        showConditionFlag: false,
      }
    },
    created() {
      this.initProgressData()
    },
    computed: {
      importExcelUrl: function () {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
      },
    },
    methods: {
      showSearchPopup(){
        this.showSearchPopupFlag = true
      },
      searchQuery(){
        if (this.queryParam.name || this.queryParam.phone || this.queryParam.address || this.currentProgressValue != 0 || this.queryParam.scrapOrderCause){
          this.showConditionFlag = true
        }else {
          this.showConditionFlag = false
        }
        this.loadData(1);
        this.selectedRowKeys = []
        this.selectedRows = []
        this.showSearchPopupFlag = false
      },
      searchReset() {
        this.showConditionFlag = false
        this.value1 = "0"
        this.value2 = "";
        this.queryParam = {}
        this.selectedRowKeys = []
        this.selectedRows = []
        this.showSearchPopupFlag = false
        this.currentProgressValue = "";
        this.loadData(1);
      },
      onSelectAll(selected, selectedRows, changeRows) {
        if (selected) {
          let rows = [...selectedRows]
          this.selectedRows = this.selectedRows.concat(rows)
        } else {
          changeRows.forEach(item => {
            const delIndex = this.selectedRows.findIndex((val) => {
              return val.id === item.id
            })
            this.selectedRows.splice(delIndex, 1)
          })
        }
      },
      onSelect(record, selected) {
        if (selected) {
          this.selectedRows.push(record)
        } else {
          const delIndex = this.selectedRows.findIndex((val) => {
            return val.id === record.id
          })
          this.selectedRows.splice(delIndex, 1)
        }
      },
      onSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },
      //根据当前进展查询数据
      changeProgress(value){
        console.log(this.queryParam.scrapOrderCause)
        if (this.queryParam.name || this.queryParam.phone || this.queryParam.address || this.queryParam.scrapOrderCause || value != 0){
          this.showConditionFlag = true
        }else {
          this.showConditionFlag = false
        }
        this.showSearchPopupFlag = false;
        this.currentProgressValue = value
        this.loadData(1)
      },
      initProgressData() {
        getAction(this.url.getItemsByDictCodeUrl).then(res => {
          if (res.success) {
            this.progressList = res.result.progressDictItemList
            for(let i=0;i<this.progressList.length;i++){
              let obj = {};
              obj.text = this.progressList[i].itemText
              obj.value = this.progressList[i].itemValue
              this.progressOption.push(obj)
            }
          }
        })
      },
      changeScrapOrderCause(value) {
        if (this.queryParam.name || this.queryParam.phone || this.queryParam.address || this.currentProgressValue != 0 || value != ""){
          this.showConditionFlag = true
        }else {
          this.showConditionFlag = false
        }
        this.showSearchPopupFlag = false;
        if (value != ""){
          this.queryParam.scrapOrderCause = value
        }else {
          delete this.queryParam.scrapOrderCause
        }
        this.loadData(1)
      },
      onLoadList(e){
        this.ipagination.current++
        this.loadData(this.ipagination.current)
      },
      loadData(arg) {
        if (!this.url.list) {
          this.$message.error('请设置url.list属性!')
          return
        }
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1
          this.dataSource = []
          this.finished = false;
        }
        let params = this.getQueryParams()//查询条件
        this.loading = true
        if (this.currentProgressValue != null && this.currentProgressValue != '' && this.currentProgressValue != 0) {
          params.progress = this.currentProgressValue
        }
        params.highSeasDepot = 3
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = [...this.dataSource, ...res.result.records]
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
          if(res.result.records.length<10){
            this.finished = true;
          }
        }).finally(() => {
          this.loading = false
        })
      },
      querySearch() {
        this.loadData(1)
      },
      resetSearch() {
        this.currentProgressValue = ''
        let progressChecked = this.progressList.filter(item => item.checkedValue === true)
        progressChecked[0].checkedValue = false
        this.progressList[0].checkedValue = true

        this.queryParam = {};
        this.loadData(1)
      },
      followRecordDetail(record) {
        this.$refs.customerFollowRecordModal.pageIntoType = 1
        this.$refs.customerFollowRecordModal.currentCustomerId = record.customerId
        this.$refs.customerFollowRecordModal.currentHighSeas = record.highSeasDepot
        this.$refs.customerFollowRecordModal.getCustomerFollowInfo()
        this.$refs.customerFollowRecordModal.visible = true
        this.$refs.customerFollowRecordModal.currentHighSeasCustomer = record
        this.$refs.customerFollowRecordModal.title = '客户 ' + record.name + ' 的跟进记录'
      },
      getScrapOrderCause(text){
        switch (text) {
          case 1:
            return "只是咨询";
          case 2:
            return "信息有误";
          case 3:
            return "同行抢单";
          case 4:
            return "价格太高";
          case 5:
            return "设计不理想";
          case 6:
            return "跟进超时";
        }
      },
      getProgressValue(value){
        if (value != null){
          let dictValue = filterDictTextByCache('customer_info_progress',value);
          if (dictValue != null && dictValue != ""){
            return dictValue;
          }else {
            return "无"
          }
        }else {
          return "无"
        }
      },
    }
  }
</script>

<style scoped>
  /deep/ .van-dropdown-menu__bar{
    height: 30px;
    box-shadow: none;
    border: 1px solid lightgrey;
    border-radius: 5px;
  }
  .list{
    overflow-y: scroll;
    height:100vh;
  }
  ::-webkit-scrollbar {
    display: none;
  }
  .customer-list {
    align-items: center;
    border-top: 1px solid #d9d9d9;
    /*height: 120px;*/
  }
  .name{
    margin-top: 10px;
    margin-bottom: 5px;
    font-size: 13px;
    color: #161616;
    line-height: normal;
  }
  .contact{
    font-size: 12px;
    color: #a39f9f;
    margin-bottom: 5px;
  }
</style>